package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.SetMealDto;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.SetMealMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.vo.CheckGroupVo;
import com.itheima.health.vo.SetMealVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
@Transactional
public class SetmealServiceimpl extends ServiceImpl<SetMealMapper, Setmeal>
        implements SetmealService {

    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * PC端套餐数据的添加和修改
     * @param setMealDto
     * @return
     */
    @Override
    public Boolean saveSetmeal(SetMealDto setMealDto) {
        //完成套餐录入  自动生成主键,制定要注意
        boolean flag = saveOrUpdate(setMealDto);
        //得到套餐的主键
        Integer id = setMealDto.getId();
        //判断得到的id是否存在
        if(StringUtils.isNotBlank(String.valueOf(id))){
            //id存在，进行修改操作
            //先删除第三张表中的id
            baseMapper.updateSetmealById(id);
        }
        //得到增加的套餐的主键
        Integer sid = setMealDto.getId();
        //得到勾选的检查组的所有id
        Integer[] checkgroupIds = setMealDto.getCheckgroupIds();
        if (checkgroupIds!=null&&checkgroupIds.length>0) {
            for (Integer checkgroupId : checkgroupIds) {
                baseMapper.addSetmeal(sid,checkgroupId);
            }
        }
        return flag;
    }



    //分页查询

    /**
     * PC端套餐分页查询
     * @param queryPageBean  分页查询参数
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //查询条件
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        Page<Setmeal> page = null;
        if(StringUtils.isNotBlank(queryPageBean.getQueryString())){
           //有条件查询
            queryWrapper.like("HELPCODE",queryPageBean.getQueryString());
          page =  page(new Page<Setmeal>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize()),queryWrapper);

        }else{
            //无条件查询
            page =  page(new Page<Setmeal>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize()),queryWrapper);
        }
        //讲数据库查询的分页参数数据封装到PageResult中
        PageResult pageResult = new PageResult(page.getTotal(),page.getRecords());
        return pageResult;
    }



    /**
     * oss中垃圾图片的清理
     */
    @Override
    public void clearOssImg() {
        //Xxjob调用
        //获取set集合中所有的图片信息
        Set<String> members = RedisUtil.getMembersOfSet(RedisConstant.ALL_SETMEAL_PIC_SET);
        //遍历集合
        if(!members.isEmpty()){
            for (String uuidfilename : members) {
                boolean flag = RedisUtil.existsKey(RedisConstant.SINGLE_PIC + uuidfilename);
                if(!flag){
                    //redis上不存在这张图片，说明图片过期,删除oss上的该图片信息
                    AliyunUtils.deleteFile(uuidfilename);
                    System.out.println("阿里云图库删除一次");
                    //再将set集合中的该图片信息删除
                    RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,uuidfilename);


                }
            }
        }

    }



    /**
     * pc端套餐数据的修改，数据的回显
     * @param id
     * @return
     */
    @Override
    public SetMealVo updatesetmealById(Integer id) {
//        //根据套餐id查询该套惨对应的图片信息
//       String img =  baseMapper.findImgById(id);
//        Setmeal setmeal = new Setmeal();
//        setmeal.setImg(img);
        //获取所有的检查组信息
        List<CheckGroup> checkGroupList = checkGroupService.list();
        //通过套餐主键查找对应的勾选检查组信息
        Integer [] checkgroupIds = baseMapper.findCheckGroupById(id);
        SetMealVo setMealVo = new SetMealVo();
        setMealVo.setCheckgroupIds(checkgroupIds);
        setMealVo.setCheckgruopList(checkGroupList);
        return setMealVo;
    }


    /**
     * 移动端套餐详情查询
     * @param id 套餐id
     * @return  套餐id对应的套餐详情信息
     */
    @Override
    public SetMealVo findSetmealById(Integer id) {
        //根据套餐id查询套餐信息
        SetMealVo setMealVo =   baseMapper.findSetmealById(id);
        //根据套餐id查询对应的检查组信息
        List<CheckGroupVo> checkGroupList =  baseMapper.findSetmealinfoByIdAndCheckGroupList(id);
        if(checkGroupList!=null&&checkGroupList.size()!=0){
            //遍历集合
            for (CheckGroupVo checkGroup : checkGroupList) {
                //拿到对应检查组的ID
                Integer gid = checkGroup.getId();
               List<CheckItem> checkItemList =  baseMapper.findCheckItemByCheckId(gid);
               checkGroup.setCheckitemList(checkItemList);
            }
        }
        //根据检查组id查询对应的检查项信息

        //将检查组的信息封装到vo对象中
        setMealVo.setCheckGroupList(checkGroupList);

        return setMealVo;
    }


}
